<?php

class DbAccess
{
	public function DbAccess()
	{
		$this->isIncr=$this->dbHandle=false;
		$this->queryStack=array();
	}

	public function connect($dbhost,$dbuser,$dbpass,$usedb)
	{
		$this->dbHandle = mysql_connect($dbhost,$dbuser,$dbpass);

		if(!$this->dbHandle)
			throw new Exception("'$dbhost' - Verbindungsfehler");

		if(!mysql_select_db($usedb, $this->dbHandle))
			throw new Exception(mysql_error($this->dbHandle));

		mysql_set_charset ("utf8", $this->dbHandle);
	}

	public function doQuery($query)
	{
		$this->curResult = mysql_query($query);
	
		if(!$this->curResult)
			throw new Exception(mysql_error($this->dbHandle));
		
		$this->isIncr=false;
	}	
	
	public function pushQuery($query)
	{
		$this->curResult = mysql_query($query);
	
		if(!$this->curResult)
			throw new Exception(mysql_error($this->dbHandle));
	
		array_push($this->queryStack, $this->curResult);
		
		$this->isIncr=false;
	}
	
	public function hasMoreResults()
	{
		$this->curResultLine = mysql_fetch_array($this->curResult);
	
		if($this->curResultLine!=false)
		{
			$this->isIncr=true;
			return true;
		}
	
		return false;
	}
	
	public function nextResultLine()
	{
		if(!$this->isIncr)
		{
			$this->curResultLine = mysql_fetch_array($this->curResult);
	
			if(!$this->curResultLine)
				throw new Exception(mysql_error($this->dbHandle));
	
			$this->isIncr=true;
		}
	
		return $this->curResultLine;
	}	
	
	public function popQuery()
	{
		if(array_pop($this->queryStack)==null)
			return new Exception("QueryStack leer.");
		
		if(!empty($this->queryStack))
		{
			$len = count($this->queryStack);
			$this->curResult=$this->queryStack[$len-1];
		}
	}
	
	public function closeConnection()
	{
		if($this->curResult!=null)
		mysql_free_result($this->curResult);

		if($this->dbHandle!=false)
		mysql_close($this->dbHandle);
	}

	private $queryStack;
	private $dbHandle;
	private $curResult;
	private $curResultLine;
	private $isIncr;
}

?>
